package timeTask

import (
	"fmt"
	"gin-demo/container"
	dao "gin-demo/dao/test"
	"gin-demo/entity"
	"github.com/robfig/cron"
	"github.com/rs/zerolog/log"
	"math/rand"
	"time"
)

func CreateTestTimeTaskInit() {
	go CreateTestTimeTask()
}

func CreateTestTimeTask() {
	c := cron.New()
	//*/2 * * * * *, 1分钟触发一次
	err := c.AddFunc("0 */1 * * * *", execTask)
	if err != nil {
		return
	}
	c.Start()

	defer c.Stop()

	select {}
}
func execTask() {
	log.Info().Msg("time task  create test exec ...")
	age := rand.Intn(100)
	daoImp := container.Get("testDao").(dao.TestDaoImp)
	create, err := daoImp.Create(&entity.Test{
		Age:        age,
		UpdateTime: time.Now().Unix(),
	})
	if err != nil {
		return
	}
	log.Info().Msg(fmt.Sprintf("create : %v", create))

}
